package me.numark.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import me.numark.R;

/**
 * Created by tian on 14-10-4:上午11:28.
 */
public class InterestCheckBox extends RelativeLayout implements View.OnClickListener {

    public void setListener(CheckStateChangedListener listener) {
        mListener = listener;
    }

    public interface CheckStateChangedListener {

        public void changeStateChanged(boolean isChecked, int id);
    }

    private CheckStateChangedListener mListener;

    public InterestCheckBox(Context context) {
        super(context);
        initView(context);
    }

    public InterestCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public InterestCheckBox(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    private TextView mTextView;

    private ImageView mImageView;

    private View mVerticalDivider;

    private View mHorizontalDivider;

    private boolean isChecked;

    public InterestCheckBox(Activity activity, ViewGroup parent) {
        super(activity);
        initView(activity);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.interest_grid_item, this);
        mTextView = (TextView) findViewById(R.id.name);
        mImageView = (ImageView) findViewById(R.id.check_box);
        mVerticalDivider = findViewById(R.id.vertical_divider);
        mHorizontalDivider = findViewById(R.id.horizontal_divider);
        mTextView.setOnClickListener(this);
        mImageView.setOnClickListener(this);
    }

    public void setText(String text) {
        mTextView.setText(text);
    }

    public void setText(int textId) {
        mTextView.setText(textId);
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean isChecked) {
        this.isChecked = isChecked;
    }

    public void toggle() {
        isChecked = !isChecked;
        mImageView.setSelected(isChecked);
        if (mListener != null) {
            int tag = (Integer)getTag();
            mListener.changeStateChanged(isChecked, tag);
        }
    }

    @Override
    public void onClick(View v) {
        toggle();
    }

    public View getHorizontalDivider() {
        return mHorizontalDivider;
    }

    public View getVerticalDivider() {
        return mVerticalDivider;
    }

}
